package com.yzh.homework;

import java.io.*;

/**
 * @author 于振晗
 * @title: CopyDirectory
 * @description: TODO 复制目录
 * @date 2020年09月07日
 * @path com.yzh.homework
 */
public class GetJavaFile {
    private static long count = 0;

    public static void main(String[] args) {
        copyDir("D:\\SXT\\myProjectCode\\JavaSE");
        System.out.println(count);
    }


    public static void copyDir(String source){
        //源文件
        File dir1 = new File(source);
        if(!dir1.exists()){
            System.out.println("源文件不存在");
            return;
        }

        File[] files = dir1.listFiles();
        for (File file : files) {
            if(file.isFile()){
                if (file.getName().contains(".")) {
                    //判断是否为java文件
                    if(file.getName().substring(file.getName().lastIndexOf("."),file.getName().length()).equals(".java")){
                        System.out.println(file.getPath());
                        count(file.getPath());
                    }
                }else{
                    return;
                }
            }else{
                copyDir(source+"/"+file.getName());
            }
        }

    }

    public static void count(String path){
        System.out.println(path);
        try(BufferedReader br = new BufferedReader(new FileReader(path))) {
            String str = "";
            while ((str = br.readLine()) != null) {
                count++;
            }
        } catch (IOException e) {
            System.out.println("错了");
        }
    }
}
